package cn.datawin.bms.modules.base.controller;

import cn.datawin.bms.base.entity.BaseNumberPortingEntity;
import cn.datawin.bms.modules.base.service.BaseNumberPortingService;
import cn.datawin.common.core.util.R;
import cn.datawin.common.log.annotation.SysLog;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 携号转网
 *
 * @author datawin-sms
 * @date 2025-09-10 19:04:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/base/baseNumberPorting" )
@Tag(description = "baseNumberPorting" , name = "携号转网管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BaseNumberPortingController {

    private final BaseNumberPortingService baseNumberPortingService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param baseNumberPorting 携号转网
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('base_baseNumberPorting_view')" )
    public R getBaseNumberPortingPage(@ParameterObject Page page, @ParameterObject BaseNumberPortingEntity baseNumberPorting) {
        LambdaQueryWrapper<BaseNumberPortingEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(baseNumberPortingService.page(page, wrapper));
    }


    /**
     * 通过id查询携号转网
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('base_baseNumberPorting_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(baseNumberPortingService.getById(id));
    }

    /**
     * 新增携号转网
     * @param baseNumberPorting 携号转网
     * @return R
     */
    @Operation(summary = "新增携号转网" , description = "新增携号转网" )
    @SysLog("新增携号转网" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('base_baseNumberPorting_add')" )
    public R save(@RequestBody BaseNumberPortingEntity baseNumberPorting) {
        return R.ok(baseNumberPortingService.save(baseNumberPorting));
    }

    /**
     * 修改携号转网
     * @param baseNumberPorting 携号转网
     * @return R
     */
    @Operation(summary = "修改携号转网" , description = "修改携号转网" )
    @SysLog("修改携号转网" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('base_baseNumberPorting_edit')" )
    public R updateById(@RequestBody BaseNumberPortingEntity baseNumberPorting) {
        return R.ok(baseNumberPortingService.updateById(baseNumberPorting));
    }

    /**
     * 通过id删除携号转网
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除携号转网" , description = "通过id删除携号转网" )
    @SysLog("通过id删除携号转网" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('base_baseNumberPorting_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(baseNumberPortingService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param baseNumberPorting 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('base_baseNumberPorting_export')" )
    public List<BaseNumberPortingEntity> export(BaseNumberPortingEntity baseNumberPorting,Long[] ids) {
        return baseNumberPortingService.list(Wrappers.lambdaQuery(baseNumberPorting).in(ArrayUtil.isNotEmpty(ids), BaseNumberPortingEntity::getId, ids));
    }


	/**
	 * 刷新缓存
	 */
	@GetMapping("/refreshCache")
	@PreAuthorize("@pms.hasPermission('base_baseNumberPorting_refreshCache')" )
	public R refreshCache() {
		new Thread(() -> baseNumberPortingService.refreshCache()).start();
		return R.ok("开始刷新缓存，请稍微");
	}
}
